import Dialog from "../../../component/vant/dialog/dialog";

let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: {},
        outStore: {},
        dynamicCargoData: [{
            "cargoNo": "",
            "wholesalePrice": "",
            "skuInfoList": []
        }],
        outStoreId: 0,
        // 打印所配置
        // 循环次数
        looptime: 0,
        // 当前次数
        currentTime: 1,
        // 最后循环次数的字节数
        lastData: 0,
        // 循环一次打印的字节数
        oneTimeData: 0,
        // 一次打印字节数list
        buffSize: [],
        // 一次打印字节数list下标值
        buffIndex: 0, //发送字节数下标
        // 打印分数选择list
        printNum: [],
        // 打印份数list下标值
        printNumIndex: 0,
        // 打印份数值
        printerNum: 1,
        // 当前打印份数
        currentPrint: 1,
        // 防止重复点击打印按钮
        isReceiptSend: false,
        // 防止重复点击查询按钮
        isQuery: false,
        imageSrc: '../../imags/wechat.png',
        jpgSrc: '../../imags/flower2.jpg',
        canvasWidth: 100,
        canvasHeight: 100,
        jpgWidth: 200,
        jpgHeight: 200,

        // printer信息
        services: [],
        serviceId: 0,
        writeCharacter: false,
        readCharacter: false,
        notifyCharacter: false,
        isScanning: false,

        //action-sheet
        actions: [
            {name: '撤销', type: "cancel"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let outStoreId = null
        if (options.outStoreId) {
            outStoreId = options.outStoreId
        } else {
            let obj = wx.getLaunchOptionsSync()
            if (obj) {
                outStoreId = obj.query.outStoreId
            }
        }
        this.data.outStoreId = outStoreId
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var list = []
        var numList = []
        var j = 0
        for (var i = 20; i < 200; i += 10) {
            list[j] = i;
            j++
        }
        for (var i = 1; i < 10; i++) {
            numList[i - 1] = i
        }
        this.setData({
            buffSize: list,
            oneTimeData: list[0],
            printNum: numList,
            printerNum: numList[0]
        })
    },
    onShow: function () {
        let outStoreId = this.data.outStoreId
        if (outStoreId > 0) {
            this.queryData(outStoreId)
        }
    },
    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/order/add/index"
            })
        } else if (type == 'cancel') {
            this.handleOrderCancel()
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    queryData: function (outStoreId) {
        let _this = this
        this.setData({
            spinShow: true
        });
        app.formGet('/app/out/store/detail?outStoreId=' + outStoreId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    outStore: res.data.outStore,
                    order: res.data.order,
                    customerName: res.data.order.receiver + "(" + res.data.order.rarea + ")",
                    dynamicCargoData: res.data.details
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    handleOrderCancel() {
        let _this = this
        Dialog.confirm({
            title: '是否确认撤销该单据？',
            message: '撤销后销售单的出库数据、库存数据将会恢复',
        })
            .then(() => {
                let data = {}
                data.outStoreId = this.data.outStoreId
                app.formPost('/app/out/store/cancel', data).then(res => {
                    _this.setData({
                        spinShow: false
                    });
                    wx.stopPullDownRefresh()
                    if (res.code === 0) {
                        app.message(res.message, 'success')
                        setTimeout(function () {
                            wx.switchTab({
                                url: '/pages/order/index-all/index-all'
                            })
                        }, 2000)
                    } else {
                        app.message(res.message, 'error')
                    }
                }).catch(e => {
                    _this.setData({
                        spinShow: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                console.log("concel")
            });


    },
})
